package com.ruoyi.order.controller;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.order.domain.PrintOrder;
import com.ruoyi.order.service.PrintOrderService;
import com.ruoyi.order.service.PayService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;

@RestController
@RequestMapping("/api/print/order")
public class PrintOrderController extends BaseController {

    @Resource
    private PrintOrderService printOrderService;

    @Resource
    private PayService payService;

    @PostMapping("/create")
    public AjaxResult create(@RequestBody PrintOrder order) {
        Long userId = getUserId();
        order.setUserId(userId);
        Long id = printOrderService.createOrder(order);
        return AjaxResult.success(id);
    }

    @GetMapping("/page")
    public TableDataInfo page(PrintOrder query,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        query.setUserId(getUserId());
        startPage();
        PageInfo<PrintOrder> page = printOrderService.pageList(query, pageNum, pageSize);
        TableDataInfo rspData = getDataTable(page.getList());
        rspData.setTotal(page.getTotal());
        return rspData;
    }

    @GetMapping("/{id}")
    public AjaxResult detail(@PathVariable Long id) {
        return AjaxResult.success(printOrderService.detail(id, getUserId()));
    }

    @PostMapping("/{id}/cancel")
    public AjaxResult cancel(@PathVariable Long id) {
        return toAjax(printOrderService.cancel(id, getUserId()));
    }

    @PostMapping("/{id}/pay/mock")
    public AjaxResult payMock(@PathVariable Long id) {
        return toAjax(printOrderService.mockPay(id, getUserId()));
    }

    @PostMapping("/{id}/pay")
    public AjaxResult pay(@PathVariable Long id) {
        PrintOrder order = printOrderService.detail(id, getUserId());
        if (!"pending".equals(order.getStatus())) {
            return AjaxResult.error("订单状态不可支付");
        }
        BigDecimal payAmount = order.getPayAmount() != null ? order.getPayAmount() : order.getTotalAmount();
        if (payAmount == null) {
            return AjaxResult.error("订单金额异常");
        }
        int amountInFen = payAmount.movePointRight(2).intValue();
        String description = order.getTitle() != null ? order.getTitle() : "打印订单支付";
        String openid = getLoginUser().getUser().getOpenId();
        if (openid == null) {
            return AjaxResult.error("用户未绑定微信");
        }
        Map<String, Object> payParams = payService.createWechatPayOrder(
                openid,
                amountInFen,
                description,
                getUserId(),
                order.getOrderNo()
        );
        return AjaxResult.success(payParams);
    }
} 